/**
 * @details: 
 * @brief: 
 * @version: 1.0
 * @author: liuri
 * @Date: 2021-10-05 03:53:40
 * @LastEditors: liuri
 * @LastEditTime: 2021-10-05 15:09:25
 * @FilePath: /TestProject/cPath/vtysh/cmd_element.c
 * @Copyright (C) 2021 liuri. All rights reserved.
 */

#include "cmd.h"



DEFUN (config_exit,
       config_exit_cmd,
       "exit",
       "Exit current mode and down to previous mode\n")
{
    return CMD_SUCCESS;
}

/**
 * @brief: 退出别名
 * @param {*}
 * @return {*}
 */
ALIAS (config_exit,
       config_quit_cmd,
       "quit",
       "Exit current mode and down to previous mode\n")


DEFUN (config_help,
       config_help_cmd,
       "help",
       "Description of the interactive help system\n")
{
    return CMD_SUCCESS;
}   


DEFUN (config_list,
       config_list_cmd,
       "list",
       "Print command list\n")
{
// unsigned int i;
// struct cmd_node *cnode = vector_slot (cmdvec, vty->node);
// struct cmd_element *cmd;

// for (i = 0; i < vector_active (cnode->cmd_vector); i++)
// {
//    if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL    && !(cmd->attr == CMD_ATTR_DEPRECATED
//        || cmd->attr == CMD_ATTR_HIDDEN))
//        vty_out (vty, "  %s%s", cmd->string,       VTY_NEWLINE);
// }
      
    return CMD_SUCCESS;
}


void cmd_element_dump(struct cmd_element *cmd)
{
    cmd_debug("cmd element info \n");
    cmd_debug("\t cmd string: %s\n", cmd->string);
    cmd_debug("\t cmd help: %s\n",   cmd->doc);
}

int cmd_matcher_match(struct cmd_element *cmd_element, vector vline, uint32_t index)
{
    uint32_t token_index;
    struct cmd_matcher matcher;
    enum matcher_rv rv = MATCHER_NO_MATCH;
    
    // 匹配器初始化
    cmd_matcher_init(&matcher, vline, index);

    for (token_index = 0; token_index < vector_max(cmd_element->tokens); token_index++)
    {
        struct cmd_token *token = vector_slot(cmd_element->tokens, token_index);
        cmd_debug("Match token type: %d; cmd: %s; desc:%s \n", token->type, token->cmd, token->desc);
        switch (token->type)
        {
            case TOKEN_TERMINAL:
            rv = cmd_matcher_match_terminal(&matcher, token);
            break;
            case TOKEN_MULTIPLE:
            rv = cmd_matcher_match_multiple(&matcher, token);
            break;
            case TOKEN_KEYWORD:
            rv = cmd_matcher_match_keyword(&matcher, token);
            break;
        }

        if (MATCHER_ERROR(rv))
            return rv;

        // 有匹配深度更高得命令，直接退出
        if (matcher.word_index > index)
            return MATCHER_OK;
    }
    return MATCHER_NO_MATCH;
}





/**
 * @brief: 安装命令行
 * @param {vector} ele_vec
 * @param {cmd_element} *cmd
 * @return {*}
 */
void install_element (vector ele_vec, struct cmd_element *cmd)
{
    vector_set (ele_vec, cmd);
    // 命令行和帮助信息解析
    if (cmd->tokens == NULL)
        cmd->tokens = cmd_parse_format(cmd->string, cmd->doc);
}


/**
 * @brief: 安装基础节点，每个节点下都有的命令
 * @param {enum node_type} node
 * @return {*}
 */
void install_default_basic (vector ele_vec)
{
    install_element (ele_vec, &config_exit_cmd);
    install_element (ele_vec, &config_quit_cmd);
    install_element (ele_vec, &config_help_cmd);
    install_element (ele_vec, &config_list_cmd);
}
